package com.geek.config.standard;

import javax.management.openmbean.CompositeData;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class User implements UserMBean {

  private String userName;

  private String password;

  public static User from(CompositeData compositeData) throws Exception {
    User user = new User();
    BeanInfo beanInfo = Introspector.getBeanInfo(user.getClass(), Object.class);
    for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
      String propertyName = propertyDescriptor.getName();
      Object propertyValue = compositeData.get(propertyName);
      Method method = propertyDescriptor.getWriteMethod();
      method.invoke(user, propertyValue);
    }
    return user;
  }

  @Override
  public String getUserName() {
    return userName;
  }

  @Override
  public void setUserName(String userName) {
    this.userName = userName;
  }

  @Override
  public String getPassword() {
    return password;
  }

  @Override
  public void setPassword(String password) {
    this.password = password;
  }

  @Override
  public String getInfo() {
    return "{userName :" + userName + ",password:" + password + "}";
  }

}
